<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>添加数据</title>
    <link rel="stylesheet" th:href="@{'/layui/css/layui.css'}" media="all">
    <script th:src="@{'/layui/layui.js'}"></script>
    <style type="text/css">
        tr,td,th{
            text-align: center;
        }
        img{
            width: 100px;
            height: 100px;
            z-index: -1;
        }
    </style>
</head>
<body style="margin-left: 100px;">
<Br>


    <Br>
    <form class="layui-form" id="frm" action="">
        <input type="hidden" name="images" id="imgurl">
        <input type="hidden" name="id" id="id">
        <div class="layui-form-item">
            <label class="layui-form-label">商品的标题</label>
            <div class="layui-input-inline">
                <input type="text" name="title" id="title" required  lay-verify="required" placeholder="请输入商品标题" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">商品单价</label>
            <div class="layui-input-inline">
                <input type="text" name="price" id="price" required  lay-verify="required" placeholder="请输入商品单价" autocomplete="off" class="layui-input">
            </div>
        </div>

        <!--图片上传开始-->
        <div class="layui-form-item">
            <label class="layui-form-label">
                <button type="button" class="layui-btn" id="test1">选择图片</button>
            </label>
            <div class="layui-input-inline">
                    <img id="demo1">
                </div>
            </div>
        </div>
        <!--图片上传结束-->
        <br>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="formDemo">修改</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
<script>
    //Demo
    layui.use(['form','jquery','layer'], function(){
        var form = layui.form;
        var $ = layui.$;
        var layer  = layui.layer;

        //监听提交
        form.on('submit(formDemo)', function(data){

            $.ajax({
                url:"http://api.shopping.com/api/item/sku/update",
                type:"put",
                data: $("#frm").serialize(),
                success:function(data){
                    layer.alert("添加成功",function () {
                        window.parent.location.reload();
                    });
                }
            });

            return false;
        });
    });
</script>

<!--选择图片时，上传图片-->

<script>
    layui.use('upload', function(){
        var $ = layui.jquery
            ,upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#test1'
            ,url: 'http://api.shopping.com/api/upload/img'
            ,auto: true
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code > 0){
                    layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功----------'+res.data);
                //把返回的上传成功图片路径返回来，存储在隐藏表单域中，准备向数据库中存储
                layer.msg(res.data);
                $("#imgurl").val(res.data);

            }
        });
    });
</script>
</body>
</html>